<?php
/***********************************************
 *      yaf 框架默认控制器
 * 文件: /controller/Index.php
 * 说明: yaf 框架默认控制器
 * 作者: Myxf
 * 更新: 2015年5月15日
 ***********************************************/

/**
 * yaf 框架默认控制器
 */
class IndexController extends Yaf_Controller_Abstract {

    private $_layout;
    private $_fun;

    public function init(){
        $this->_layout = Yaf_Registry::get('layout');
    }
	/**
	 * 留言主页面
	 */
    public function indexAction() {
        $blog = new BlogModel();
        $smarty = $this->getView();
        
        $page = intval(Fun::getg('page', 1));
        $pageSize = 3;
        $pageLimit = $pageSize*($page-1);
        $result = $blog->getAll("*","","",$pageLimit.",".$pageSize);
        $total = $blog->getCount();
        $_pager=Fun::page($total,$pageSize,$page,10,"index");
        
        $smarty->meta_title = 'Myxf留言板';
        $smarty->pager=$_pager;
        $smarty->lists=$result;


    }
    /**
     * 增加留言
     */
    public function addAction(){
    	if ($_POST){
	    	$bind = array(
	    		'name'=>$_POST['msg_name'],
	    		'content'=>$_POST['msg_content']
	    	);
	    	$blog = new BlogModel();
	    	$result = $blog->add($bind);
            
	   		if ($result){
	    		$this->_view->msg = "增加成功";
	    	}else{
	    		$this->_view->msg = "增加失败";
	    	}
    	}
    }
   /**
    * 编辑留言
    */
	public function editAction() {
		if ($_POST){
            $bind = array('name' =>$_POST['msg_name'],
                          'content' =>$_POST['msg_centent'],
                          'times' =>time()
                    );
	    	$blog = new BlogModel();
            $id = $_POST['msg_id'];
	    	$result = $blog->edit($bind, "`id` = ".$id);
	    	if ($result){
	    		$this->_view->msg = "修改成功";
	    	}else{
	    		$this->_view->msg = "修改失败";
	    	}
		}else {
			$blog = new BlogModel();
			$id = Fun::getg('id');
			$msgOne = $blog->getByid($id);
            $smarty = $this->getView();
            $smarty->msgOne = $msgOne;
		}
    }
    /**
     * 删除留言
     */
    public function deleAction(){
    	$blog = new BlogModel();
    	$id = Fun::getg('id');
    	$result = $blog->del("`id` = '$id'");
    	if ($result){
    		echo "删除成功";
    	}else{
    		echo "删除失败";
    	}
    	exit();
    }
}
